package com.ayu.wxgzh.ayucyy.Controller;

import com.alibaba.fastjson.JSONObject;
import com.ayu.wxgzh.ayucyy.CusException.AesException;
import com.ayu.wxgzh.ayucyy.Utils.SHA1;
import com.ayu.wxgzh.ayucyy.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 徐林玉
 * @Description WeixinWebAuthServiceController
 * @Date 2022/8/29 10:54
 * @Version 1.0
 */
@RestController
@Slf4j
public class WeixinWebAuthController {


    private final MessageService messageService;

    private final ResourceLoader resourceLoader;



    @Autowired
    public WeixinWebAuthController(MessageService messageService, ResourceLoader resourceLoader) {
        this.messageService = messageService;
        this.resourceLoader = resourceLoader;
    }


    @GetMapping("/")
    public String authGet(@RequestParam(name = "signature", required = false) String signature,
                          @RequestParam(name = "timestamp", required = false) String timestamp,
                          @RequestParam(name = "nonce", required = false) String nonce,
                          @RequestParam(name = "echostr", required = false) String echostr) {

        log.info("\n接收到来自微信服务器的认证消息：[{}, {}, {}, {}]", signature,
                timestamp, nonce, echostr);
        String ayucyy = "";
        try {
            ayucyy = SHA1.getSHA1("cyyayu", timestamp, nonce, "");
        } catch (AesException e) {
            log.info(e.getMessage());
        }
        log.info("验证消息的确来自微信服务器 :加密" + ayucyy);
        log.info("验证消息的确来自微信服务器 :本身" + signature);

        if (ayucyy.equals(signature)) {
            return echostr;
        } else {

            return "";
        }

    }

    @PostMapping("/")
    public String getMesssage(HttpServletRequest request, HttpServletResponse response) {

        String returnMessage = messageService.messageHandle(request, response);

        return returnMessage;

    }









}
